package com.normal.main.vm

import android.app.Application
import com.normal.zbase.impl.base.BaseVM
import com.normal.zbase.impl.enums.StorageLocEnum
import com.normal.zbase.impl.http.ApiLoginFactory
import com.normal.zbase.impl.http.apiResponse
import com.normal.zbase.impl.http.impl.bean.ChannelStatusInfoBean
import com.normal.zbase.impl.http.impl.bean.LoginResultBean
import com.normal.zbase.impl.http.requestApi
import com.normal.zbase.impl.tools.obj.DowloadManager
import com.normal.zbase.impl.tools.utils.file.FileUtil
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow


/**
 * Created by zsf on 2022/10/17 10:13
 * ******************************************
 * 持久化页面状态
 * 提供对业务逻辑的访问权限
 * ******************************************
 */
class MainActVM(application: Application) : BaseVM(application) {
    private var _channelStatusFlow: MutableSharedFlow<ChannelStatusInfoBean> = MutableSharedFlow()
    var channelStatusFlow: SharedFlow<ChannelStatusInfoBean> = _channelStatusFlow

    private var _loginResultFlow: MutableSharedFlow<LoginResultBean> = MutableSharedFlow()
    var loginResultFlow: SharedFlow<LoginResultBean> = _loginResultFlow

    /**
     * 下载图片
     */
    fun downloadFile() {
        val imgPath =
            "https://yn.hnpc.cc/ynfile/group5/hnjs/user/f6a1894a-c64e-4d73-a577-e95dd9d7b60011221.jpg"
        val file = FileUtil.createFile(StorageLocEnum.SANDBOX, "${System.currentTimeMillis()}.jpg")
        DowloadManager.downloadImg(imgPath, file, {}, {})
    }


    /**
     * POSt请求示例
     */
    fun onPost() {
        val map: MutableMap<String, Any> = HashMap()
        map["userName"] = "11111111112"
        map["password"] = "111111"
        map["channel"] = "wzsz"
        map["system"] = "customer"
        map["expiry"] = "-1"

        requestApi({
            val loginResultBean = ApiLoginFactory.queryOrder("20", "1").apiResponse()
            _loginResultFlow.emit(loginResultBean)
        }, {
            _channelStatusFlow.emit(ChannelStatusInfoBean(message = it.message))
        }, isShowLoading = true)


        requestApi({
            //请求1
            val channelStatusInfoBean =
                ApiLoginFactory.selectChannelStatusSync("nale1").apiResponse()
            _channelStatusFlow.emit(channelStatusInfoBean)
            //请求2
            val loginResultBean = ApiLoginFactory.queryOrder("20", "1").apiResponse()
            _loginResultFlow.emit(loginResultBean)
        }, {
            _channelStatusFlow.emit(ChannelStatusInfoBean(message = it.message))
        }, isShowLoading = true)


        //同步调用-连续的请求
//        viewModelScope.launch(apiException {
//            TToast.show(it.message)
//        }) {
//            val channelStatusInfoBean =
//                ApiLoginFactory.selectChannelStatusSync("nale1").apiResponse()
//            channelStatusInfoBeanLD.value = channelStatusInfoBean
//
//            val loginResultBean = ApiLoginFactory.loginSync(map).apiResponse()
//            loginResultDtoLD.value = loginResultBean
//        }


        //异步调用
//        ApiLoginFactory.login(map).execute(object : ApiSubscriber<LoginResultBean>() {
//            override fun onSuccessHandler(loginResultDto: LoginResultBean) {
//                super.onSuccessHandler(loginResultDto)
//                loginResultDtoLD.value = loginResultDto
//            }
//        })


//        ApiLoginService
//            .post(HttpApi.login)
//            /**  可选项  start  */
//            //可选 重置HostUrl
////            .host("http://10.10.10.139")
//            //可选 绑定指定的Actiivt，不填默绑定最上层栈的activity，置空不绑定生命周期，绑定了默认跟随activiti销毁而销毁
//            .bindLifecycleOwner(
//                StackManager.getActivityManager().currentActivity()
//            )
//            //可选，是否是表单提交，默认false
//            .isForm(false)
//            /**  可选项   end  */
//            .body(map)
//            .execute(LoginResultBean::class.java) //可以使用父类的code（使用继承关系），也可以使用子类的code
//            .subscribe(object : ApiSubscriber<LoginResultBean>(true) {
////                override fun onSuccessHandler(loginResultDto: LoginResultDto) {
////                    super.onSuccessHandler(loginResultDto)
////                    loginResultDtoLD.value = loginResultDto
////                }
////
////                override fun onFailHandler(loginResultDto: LoginResultDto) {
////                    super.onFailHandler(loginResultDto)
////                }
//
////*********************以上跟以上两种都行，二选一
////*********************上面那种是自动确定code = 0000的，
////*********************下面这种是不帮解析code = 0000，直接返回原生数据，需要自行判断
//
//                override fun onCompleteHandler() { //非必实现，整个请求完成的回调，方便做刷新动画的结束标志等
//                    super.onCompleteHandler()
//                }
//
//                override fun onResponseHandler(loginResultDto: LoginResultBean) { //非必实现，不判断成功还是失败，直觉返回，跟onSuccess、onFail互斥，二选一
//                    super.onResponseHandler(loginResultDto)
//                    loginResultDtoLD.value = loginResultDto
//                }
//            })
    }

    /**
     * Get请求示例
     */
    fun onGet() {

        requestApi({
            val loginResultBean = ApiLoginFactory.queryOrder("20", "1").apiResponse()
            _loginResultFlow.emit(loginResultBean)
        }, {
            _loginResultFlow.emit(LoginResultBean(message = it.message))
        })

//        ApiLoginFactory.selectChannelStatus("nale")
//            .execute(object : ApiSubscriber<ChannelStatusInfoBean>() {
//                override fun onSuccessHandler(t: ChannelStatusInfoBean) {
//                    super.onSuccessHandler(t)
////                    channelStatusInfoBeanLDFlow.emit(t)
//                }
//
//                override fun onFailHandler(t: ChannelStatusInfoBean) {
//                    super.onFailHandler(t)
//                }
//            })
    }
}
